package com.fsi.client;

import java.util.List;

import com.fsi.client.util.MyPhoto;
import com.fsi.client.util.PhotoPopup;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;

public class ShowPhotosGUI extends ServiceGUI implements ClickHandler {
	private static int v = 0;
	private int i = 0;
	ScrollPanel photoScroll;
	HorizontalPanel content;
	FlexTable photoTable;
	MyPhoto clone;
	Label noPhotosLbl = new Label("No photos");
	private String customerID;

	public ShowPhotosGUI() {
		photoScroll = new ScrollPanel();
		content = new HorizontalPanel();

		photoTable = new FlexTable();

		content.add(photoTable);
		photoScroll.add(content);

		this.add(noPhotosLbl);
		this.add(photoScroll);

		// photoScroll.setStyleName("test");
		photoScroll.setWidth("900px");
		noPhotosLbl.setVisible(false);
	}

	public void loadGui(String customerID) {
		photoTable.clear();
		service.getPhotos(this, customerID);
		this.customerID = customerID;
	}

	@Override
	public void service_getPhotoIDs(final List<Shuttle> photos) {
		i = 0;
		if (photos != null && !photos.isEmpty()) {
			noPhotosLbl.setVisible(false);
			for (Shuttle details : photos) {
				final MyPhoto p = new MyPhoto(details.getPhotoID(), details
						.getTitle());
				p.setVisible(false);
				p.addClickHandler(this);
				p.setTitle("Click to Enlarge");
				p.setUrl(GWT.getModuleBaseURL() + "getphoto?nick="
						+ details.getPhotoID() + "&var=" + v++);
				photoTable.setWidget(0, i++, p);
				p.addLoadHandler(new LoadHandler() {

					@Override
					public void onLoad(LoadEvent event) {
						MyPhoto ep = (MyPhoto) event.getSource();
						p.setOriginalHeight(ep.getHeight());
						p.setOriginalWidth(ep.getWidth());
						p.setHeight("150px");
						p.setVisible(true);
					}
				});
			}
		} else {
			noPhotosLbl.setVisible(true);
		}
	}

	@Override
	public void onClick(ClickEvent event) {
		MyPhoto photo = (MyPhoto) event.getSource();
		new PhotoPopup(customerID, photo.getName(), photo.getPhotoTitle(),
				photo.getOriginalHeight(), photo.getOriginalWidth());
	}
}
